/**
 * 数据库信息配置相关JS
 */

$(function () {
    // 初始化表格
    var dg = $('#dg').datagrid({
        url: basePath + "/rptTools/sys/queryVersionInfo",
        singleSelect: true,
        rownumbers:true,
        striped: true,
        remoteSort: true,
        pagination:true,//显示分页
        pageSize: 20,
        columns:[
            [
                {field:'versionId',title:'版本ID',width:320},
                {field:'createDate',title:'更新时间',width:350},
                {field:'operate',title:'操作',width:350,align:'center',formatter:function(value,row,index){
                    var $row = JSON.stringify(row).replace(/\"/g,"'");
                    return '<a style="color:#4697e5; text-decoration:none;padding-right:15px;" onClick="versionCfg.editVersion(' +  $row + ')" href="#">修改版本</a>'
                         + '<a style="color:#4697e5; text-decoration:none;padding-left:10px;" onClick="versionCfg.delVersion(\'' +  row.versionId + '\',\'' + row.createDate +'\'' + ')" href="#">删除版本</a>'
                         + '<a style="color:#4697e5; text-decoration:none;padding-left:35px;" onClick="versionCfg.queryVersionDetail(\'' +  row.versionId + '\'' + ')" href="#">版本明细</a>'
                }}
            ]
        ]
    });
});

var versionCfg = {
    /**
     * 查询数据
     */
    query : function() {
        var versionId = $('#versionId').val();

        var param = {};
        param.versionId = versionId;
        $('#dg').datagrid({
            url:basePath + "/rptTools/sys/queryVersionInfo",
            queryParams:param
        });
    },
    addVersion : function () {
        this.resetWin();
        $('#addVersionWin').window('open');

    },
    editVersion : function (versionInfo) {
        this.resetEditWin();
        var versionId = versionInfo.versionId;
        var param = {};
        param.versionId = versionId;
        $.ajax({
            url:basePath + "/rptTools/sys/versionDetail",
            type:'POST',
            dataType:'json',
            data: param,
            async: false,
            success:function(data){
                var status = data.status;
                var errorMsg = data.errorMsg;
                if(status == "1") {
                    var versionDesc = data.version.versionDesc;
                    $("#editVersionId").textbox('setValue',versionId);
                    $("#editVersionDesc").textbox('setValue',versionDesc);
                    $('#editVersionWin').window('open');
                } else {
                    alert(errorMsg);
                }
            }
        })
    },
    queryVersionDetail : function (versionId) {
        window.open(basePath + "/rptTools/sys/version?versionId=" + versionId);
    },
    delVersion : function (versionId, createDate) {
        var confirmMsg = '确认删除版本[' + versionId + ']吗?'
        var func = this;
        $.messager.confirm('提示', confirmMsg, function(r){
            if (r){
                func.showMask();
                $.ajax({
                    url:basePath + "/rptTools/sys/deleteVersion",
                    type:'POST',
                    dataType:'json',
                    data:{
                        versionId: versionId
                    },
                    async: false,
                    success:function(data){
                        $("#mask").hide();
                        var status = data.status;
                        var errorMsg = data.errorMsg;
                        if(status == "1") {
                            alert("删除版本成功");
                            $('#dg').datagrid({
                                url:basePath + "/rptTools/sys/queryVersionInfo"
                            });
                        } else {
                            alert(errorMsg);
                        }
                    }
                })
            }
        });
    },
    showMask : function () {
        $("#mask").css("height",$(document).height());
        $("#mask").css("width",$(document).width());
        $("#mask").show();
    },
    saveVersion : function () {
        var errorMsg = '';
        var errorFlag = false;
        var versionId = $("#instVersionId").val();
        var versionDesc = $("#instVersionDesc").val();
        if(versionId == '') {
            errorMsg = '版本ID不能为空';
            errorFlag = true;
        } else if(versionDesc == '') {
            errorMsg = '版本描述不能为空';
            errorFlag = true;
        }
        if(errorFlag) {
            alert(errorMsg);
            return;
        }

        var param = {};
        param.versionId = versionId;
        param.versionDesc = versionDesc;
        $.ajax({
            url:basePath + "/rptTools/sys/addVersionInfo",
            type:'POST',
            dataType:'json',
            data: param,
            async: false,
            success:function(data){
                var status = data.status;
                var errorMsg = data.errorMsg;
                if(status == "1") {
                    alert("新增版本成功");
                    $('#addVersionWin').window('close');
                    $('#dg').datagrid({
                        url:basePath + "/rptTools/sys/queryVersionInfo"
                    });
                } else {
                    alert(errorMsg);
                }
            }
        })
    },
    updateVersion : function () {
        var errorMsg = '';
        var errorFlag = false;
        var versionId = $("#editVersionId").val();
        var versionDesc = $("#editVersionDesc").val();
        if(versionDesc == '') {
            errorMsg = '版本描述不能为空';
            errorFlag = true;
        }
        if(errorFlag) {
            alert(errorMsg);
            return;
        }

        var param = {};
        param.versionId = versionId;
        param.versionDesc = versionDesc;
        $.ajax({
            url:basePath + "/rptTools/sys/updateVersionInfo",
            type:'POST',
            dataType:'json',
            data: param,
            async: false,
            success:function(data){
                var status = data.status;
                var errorMsg = data.errorMsg;
                if(status == "1") {
                    alert("版本信息修改成功");
                    $('#editVersionWin').window('close');
                    $('#dg').datagrid({
                        url:basePath + "/rptTools/sys/queryVersionInfo"
                    });
                } else {
                    alert(errorMsg);
                }
            }
        })
    },
    resetWin : function () {
        $("#instVersionId").textbox('setValue','');
        $("#instVersionDesc").textbox('setValue','');
    },
    resetEditWin : function () {
        $("#editVersionId").textbox('setValue','');
        $("#editVersionDesc").textbox('setValue','');
    },
    closeWin : function (winId) {
        $('#' + winId).window('close');
    }
}
